﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using FriendStar.Domain.IRepository.BOM;
using FriendStar.Repository.BOM;

namespace FriendStar.Application.BOM
{
    /// <summary>
	/// 数据视图 V_PLM_CP_ProductSum 对应的数据应用功能类。
	/// </summary>
	public class V_PLM_CX_ProductSumApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private IV_PLM_CX_ProductSumRepository service = new V_PLM_CX_ProductSumRepository();
        private V_PLM_CP_ProductDetailRepository detailService = new V_PLM_CP_ProductDetailRepository();

        public List<V_PLM_CX_ProductSumEntity> GetList(Pagination pagination, string number, string name, string assemble, string make)
        {
            var expression = ExtLinq.True<V_PLM_CX_ProductSumEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(number))
            {
                expression = expression.And(t => t.FProductionNO.Contains(number));
            }
            if (!string.IsNullOrEmpty(name))
            {
                expression = expression.And(t => t.MC.Contains(name));
            }
            if (!string.IsNullOrEmpty(assemble))
            {
                expression = expression.And(t => t.AssembleLine.Contains(assemble));
            }
            if (!string.IsNullOrEmpty(make))
            {
                expression = expression.And(t => t.MakeLine.Contains(make));
            }
            return service.FindList(expression, pagination);
        }

        public List<V_PLM_CX_ProductSumEntity> GetList(Pagination pagination, string number, string name, string assemble, string make, string orderNum)
        {
            if (string.IsNullOrEmpty(orderNum)) 
            {
                return new List<V_PLM_CX_ProductSumEntity>();
            }


            var expression = ExtLinq.True<V_PLM_CX_ProductSumEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(number))
            {
                expression = expression.And(t => t.FProductionNO.Contains(number));
            }
            if (!string.IsNullOrEmpty(name))
            {
                expression = expression.And(t => t.MC.Contains(name));
            }
            if (!string.IsNullOrEmpty(assemble))
            {
                expression = expression.And(t => t.AssembleLine.Contains(assemble));
            }
            if (!string.IsNullOrEmpty(make))
            {
                expression = expression.And(t => t.MakeLine.Contains(make));
            }
            string extWhere = orderNum + ">0";
            return service.FindList(expression, pagination, extWhere);
        }

        public List<V_PLM_CP_ProductDetailEntity> GetDetailList(string number)
        {
            string strSql = string.Format("EXEC PROC_PLM_CX_ProductDetail '{0}'", number);
            List<V_PLM_CP_ProductDetailEntity> List = detailService.FindList(strSql);
            return List;
        }

        public V_PLM_CX_ProductSumEntity GetItem(string number)
        {
            var expression = ExtLinq.True<V_PLM_CX_ProductSumEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            if (!string.IsNullOrEmpty(number))
            {
                expression = expression.And(t => t.FProductionNO == number);
            }
            return service.FindEntity(expression);
        }

        public V_PLM_CX_ProductSumEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
    }
}